package com.ssy.lingxi.marketing.serviceimpl;

import com.ssy.lingxi.common.constant.marketing.ActivityTypeEnum;
import com.ssy.lingxi.common.constant.marketing.BelongTypeEnum;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.marketing.entity.activity.MerchantActivityDO;
import com.ssy.lingxi.marketing.model.constant.MerchantActivityInnerStatusEnum;
import com.ssy.lingxi.marketing.model.vo.activity.request.MerchantActivityPlatformPageVO;
import com.ssy.lingxi.marketing.model.vo.activity.request.PfActivityGoodsPageReq;
import com.ssy.lingxi.marketing.model.vo.activity.response.*;
import com.ssy.lingxi.marketing.model.vo.common.request.CommonIdReq;
import com.ssy.lingxi.marketing.model.vo.common.response.TaskStepVO;
import com.ssy.lingxi.marketing.repository.MerchantActivityRepository;
import com.ssy.lingxi.marketing.service.IPlatformMerchantActivityService;
import com.ssy.lingxi.marketing.serviceimpl.base.activity.BaseActivityGoodsService;
import com.ssy.lingxi.marketing.serviceimpl.base.activity.BaseActivityRecordService;
import com.ssy.lingxi.marketing.serviceimpl.base.activity.BaseActivityRuleService;
import com.ssy.lingxi.marketing.serviceimpl.base.activity.BaseMerchantActivityService;
import com.ssy.lingxi.marketing.serviceimpl.component.SuitableFieldComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 平台后台 - 商家活动服务实现类
 * @author yzc
 * @version 2.0.0
 * @date 2021/8/17
 */
@Slf4j
@Service
public class PlatformMerchantActivityServiceImpl extends BaseMerchantActivityService implements IPlatformMerchantActivityService {

    @Resource
    private MerchantActivityRepository merchantActivityRepository;
    @Resource
    private BaseActivityRecordService activityRecordService;
    @Resource
    private BaseActivityGoodsService activityGoodsService;
    @Resource
    private BaseActivityRuleService activityRuleService;

    @Override
    public Wrapper<PageData<PlatformMerchantActivityPageResp>> pageSummary(UserLoginCacheDTO loginUser, MerchantActivityPlatformPageVO pageVO) {
        Page<MerchantActivityDO> result = this.basePlatformMerchantActivityPage(loginUser, pageVO, pageVO.getOuterStatus());

        List<PlatformMerchantActivityPageResp> resultList = result.stream().map(e -> {
            PlatformMerchantActivityPageResp resp = new PlatformMerchantActivityPageResp();
            resp.setId(e.getId());
            resp.setActivityName(e.getActivityName());
            resp.setActivityType(e.getActivityType());
            resp.setActivityTypeName(ActivityTypeEnum.getMessage(e.getActivityType()));
            resp.setMemberName(e.getMemberName());
            resp.setStartTime(e.getStartTime());
            resp.setEndTime(e.getEndTime());
            resp.setInnerStatus(e.getInnerStatus());
            resp.setInnerStatusName(MerchantActivityInnerStatusEnum.getMessage(e.getInnerStatus()));
            return resp;
        }).collect(Collectors.toList());

        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    @Override
    public Wrapper<MerchantActivityDetailResp> detail(UserLoginCacheDTO loginUser, CommonIdReq request) {
        MerchantActivityDO merchantActivityDO = merchantActivityRepository.findById(request.getId()).orElse(null);
        if (Objects.isNull(merchantActivityDO)) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
        }

        MerchantActivityDetailResp detailResp = new MerchantActivityDetailResp();
        detailResp.setId(merchantActivityDO.getId());
        detailResp.setActivityType(merchantActivityDO.getActivityType());
        detailResp.setActivityTypeName(ActivityTypeEnum.getMessage(merchantActivityDO.getActivityType()));
        detailResp.setActivityName(merchantActivityDO.getActivityName());
        detailResp.setStartTime(merchantActivityDO.getStartTime());
        detailResp.setEndTime(merchantActivityDO.getEndTime());
        detailResp.setMemberName(merchantActivityDO.getMemberName());
        detailResp.setCreateTime(merchantActivityDO.getCreateTime());
        detailResp.setNewUser(SuitableFieldComponent.parseActivitySuitableUser(merchantActivityDO.getSuitableUser(), 1));
        detailResp.setOldUser(SuitableFieldComponent.parseActivitySuitableUser(merchantActivityDO.getSuitableUser(), 2));
        detailResp.setNewMember(SuitableFieldComponent.parseActivitySuitableUser(merchantActivityDO.getSuitableUser(), 3));
        detailResp.setOldMember(SuitableFieldComponent.parseActivitySuitableUser(merchantActivityDO.getSuitableUser(), 4));
        detailResp.setInnerStatus(merchantActivityDO.getInnerStatus());
        detailResp.setInnerStatusName(MerchantActivityInnerStatusEnum.getMessage(merchantActivityDO.getInnerStatus()));
        detailResp.setInnerTaskId(merchantActivityDO.getInnerTaskId());
        detailResp.setInnerProcessKey(merchantActivityDO.getInnerProcessKey());
        detailResp.setInnerTaskStep(merchantActivityDO.getInnerTaskStep());
        detailResp.setActivityDefined(getActivityDefine(BelongTypeEnum.MERCHANT, merchantActivityDO.getActivityType(), merchantActivityDO.getActivityDefined()));

        // 适用会员等级
        List<ActivityMemberLevelResp> memberLevelRespList = activityRuleService.listMemberLevel(BelongTypeEnum.MERCHANT, merchantActivityDO.getId());
        detailResp.setMemberLevelList(memberLevelRespList);

        // 适用商城
        List<ActivityShopResp> shopRespList = activityRuleService.listShop(BelongTypeEnum.MERCHANT, merchantActivityDO.getId());
        detailResp.setShopList(shopRespList);

        // 查询外部工作流程
        /*
        List<TaskStepVO> outerTaskList = activityRecordService.listOuterTask(merchantActivityDO.getOuterProcessKey(), merchantActivityDO.getOuterTaskId());
        detailResp.setOuterTaskList(outerTaskList);*/

        // 查询内部工作流程
        List<TaskStepVO> innerTaskList = activityRecordService.listInnerTask(merchantActivityDO.getInnerProcessKey(), merchantActivityDO.getInnerTaskId());
        detailResp.setInnerTaskList(innerTaskList);

        // 封装外部流转记录
        /*
        List<ActivityOuterRecordResp> merchantOuterRecord = activityRecordService.getMerchantOuterRecord(loginUser, merchantActivityDO.getId());
        detailResp.setOuterRecordDOList(merchantOuterRecord);*/

        // 封装内部流转记录
        List<ActivityInnerRecordResp> merchantInnerRecord = activityRecordService.getMerchantInnerRecord(loginUser, merchantActivityDO.getId());
        detailResp.setInnerRecordDOList(merchantInnerRecord);

        return Wrapper.success(detailResp);
    }

    @Override
    public Wrapper<PageData<McActivityGoodsPageResp>> pageByActivityGoods(UserLoginCacheDTO loginUser, PfActivityGoodsPageReq pageVO) {
        MerchantActivityDO merchantActivityDO = merchantActivityRepository.findById(pageVO.getActivityId()).orElse(null);
        if (Objects.isNull(merchantActivityDO)) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
        }

        // 平台进行查询, 不用传入memberId, roleId
        return activityGoodsService.basePageMerchantActivityGoods(null, null, pageVO.getActivityId(), pageVO.getCurrent(), pageVO.getPageSize(),merchantActivityDO.getActivityType());
    }

/*22.1.20版废弃
    @Override
    public Wrapper<Void> examine(UserLoginCacheDTO loginUser, ActivityExamineReq request) {
        MerchantActivityDO merchantActivityDO = merchantActivityRepository.findById(request.getId()).orElse(null);
        if (Objects.isNull(merchantActivityDO)) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
        }

        if (!merchantActivityDO.getOuterStatus().equals(MerchantActivityOuterStatusEnum.TO_BE_EXAM.getCode())) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_CURRENT_STATUS_NOT_ALLOW_UPDATE);
        }

        this.baseExamine(loginUser, Collections.singletonList(merchantActivityDO), request.getIsPass());

        return Wrapper.success();
    }

    @Override
    public Wrapper<Void> batchExamine(UserLoginCacheDTO loginUser, CommonIdsReq req) {
        List<MerchantActivityDO> merchantActivityDOList = merchantActivityRepository.findAllById(req.getIds());
        if (CollectionUtils.isEmpty(merchantActivityDOList)) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
        }

        for (MerchantActivityDO merchantActivityDO : merchantActivityDOList) {
            if (Objects.isNull(merchantActivityDO)) {
                return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
            }
            if (!merchantActivityDO.getOuterStatus().equals(MerchantActivityOuterStatusEnum.TO_BE_EXAM.getCode())) {
                return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_CURRENT_STATUS_NOT_ALLOW_UPDATE);
            }
        }

        this.baseExamine(loginUser, merchantActivityDOList, ProcessTaskStatusEnum.AGREE.getCode());

        return Wrapper.success();
    }*/
}
